const Area = require('../../utils/area.js');
const app = getApp()


Component({
	 options: {
    addGlobalClass: true,
  },
	properties: {
		type:{
			type:String,
			value:'general'
		},
		province_id: {
			type: Number,
			value: ''
		},
		city_id: {
			type: Number,
			value: ''
		},
		area_id: {
			type: Number,
			value: ''
		},
		disabled:{
			type: Boolean,
			value: ''
		},
		service_zone:{
			type: Array,
			value: []
  		},
	},
	data: {
		areassq: '',
		multiIndex1: [0, 0, 0],
		region: '请选择地区',
		province_id: '',
		city_id: '',
		area_id: '',
		province_name: '',
		city_name: '',
		area_name: '',
	},
	methods: {
			// 选择地区
			bindMultiPickerChange1: function(e) {//点击确定按钮的时候触发的函数
				// this.setData({
				// 	multiIndex1: [0, 0, 0]
				// })
				var column1Index = e.detail.value[0];
				var column2Index = e.detail.value[1];
				var column3Index = e.detail.value[2];
				var col1ID = this.data.areassq[0][column1Index].id;
				var col2ID = this.data.areassq[1][column2Index].id;
				var col3ID = this.data.areassq[2][column3Index].id;
				this.setData({
					region: this.data.areassq[0][column1Index].name + ',' + this.data.areassq[1][column2Index].name + ',' + this.data.areassq[2][column3Index].name,
					province_id: col1ID,
					city_id: col2ID,
					area_id: col3ID,
					province_name: this.data.areassq[0][column1Index].name,
					city_name: this.data.areassq[1][column2Index].name,
					area_name: this.data.areassq[2][column3Index].name,
					multiIndex1: [e.detail.value[0], e.detail.value[1], e.detail.value[2]],
				})
				this.triggerEvent('areaChooseSuccess',{region:this.data.region,
													   province_id:this.data.province_id,
													   city_id:this.data.city_id,
													   area_id:this.data.area_id,
													   province_name: this.data.province_name,
													   city_name: this.data.city_name,
													   area_name: this.data.area_name,
													});
			},
			bindMultiPickerColumnChange1: function(e) {//省市区发生滚动时触发的函数
				var obj = this.data.areassq[e.detail.column][e.detail.value];
				var data = {
					areassq: this.data.areassq,
					multiIndex1: this.data.multiIndex1,
				};
				data.multiIndex1[e.detail.column] = e.detail.value;
				if (e.detail.column == 0) {
					var areassq1 = 'areassq[' + 1 + ']';
					var areassq2 = 'areassq[' + 2 + ']';
					data.multiIndex1[1] = 0;
					data.multiIndex1[2] = 0;
					this.setData({
						[areassq1]: obj.next,
						[areassq2]: obj.next[0].next,
						multiIndex1: this.data.multiIndex1,
					})
				}
				console.log(this.data.multiIndex1)
				if (e.detail.column == 1) {
					var areassq2 = 'areassq[' + 2 + ']';
					this.setData({
						[areassq2]: obj.next,
						multiIndex1: this.data.multiIndex1,
					})
				}
			},
			// -------------------------------------------------------------------------
	},
	ready:function(){
		
		var multiIndex1 = [0,0,0];
		var region = []
		// 地区的数据
		var obj = Area.getAreaObj();
		var province_id = this.data.province_id;
		var city_id = this.data.city_id;
		var area_id = this.data.area_id;
		var type = this.data.type;
		if(type == "definition") {
				obj = obj.filter(d => {return this.data.service_zone.includes(d.id)});
					var mulArr1 = [];
					mulArr1.push(obj);
					if(obj[0].next) {
						mulArr1.push(obj[0].next);
				}if(obj[0].next[0].next) {
					mulArr1.push(obj[0].next[0].next);
				}
		}
		//编辑情况
		if(province_id && city_id && area_id) {
			var mulArr1 = [];
			var currProvince;
			for(let i = 0;i<obj.length;i++) {
				if(province_id == obj[i].id) {
					currProvince = obj[i];
					multiIndex1.push(i);
					region.push(obj[i].name)
					console.log(obj[i])
					break;
				} 
			}
			mulArr1.push(obj);
			if (currProvince.next) {
				var currCity;
				mulArr1.push(currProvince.next);
				for(let j = 0;j<currProvince.next.length;j++) {
					if(city_id == currProvince.next[j].id) {
						currCity = currProvince.next[j];
						multiIndex1.push(j);
						region.push(currProvince.next[j].name)
						break;
					}
				}
				mulArr1.push(currCity.next);
				for(let k = 0 ;k<currCity.next.length;k++) {
					if(area_id == currCity.next[k].id) {
						multiIndex1.push(k);
						region.push(currCity.next[k].name)
						break;
					}
				}
			}
				this.setData({
				region:region,
			});
		}
		var mulArr1 = [];
		mulArr1.push(obj);
		if (obj[0].next) {
			mulArr1.push(obj[0].next);
		}
		if (obj[0].next[0].next) {
			mulArr1.push(obj[0].next[0].next);
		}
		this.setData({
			areassq: mulArr1,
			province_id: obj[0].id,
		});
		
	},
})